package com.mcx.modules.data.base;

import com.mcx.modules.data.GameDataTableParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public abstract class AbstractDBModel<T, P extends AbstractEntity> {
    protected List<P> m_List;
    protected HashMap<Integer, P> m_Dic;

    public AbstractDBModel()
    {
        m_List = new ArrayList<P>();
        m_Dic = new HashMap<Integer, P>();

        LoadData();
    }

    /// <summary>
    /// 数据文件名称
    /// </summary>
    protected abstract String FileName();

    /// <summary>
    /// 创建实体
    /// </summary>
    /// <param name="parse"></param>
    /// <returns></returns>
    protected abstract P MakeEntity(GameDataTableParser parse);

    /// <summary>
    /// 加载数据
    /// </summary>
    private void LoadData()
    {
        GameDataTableParser parse = new GameDataTableParser(String.format("F:\\UnityProject\\Monster Impact Excel\\%s", FileName()));

        while (!parse.Eof())
        {
            //创建实体
            P p = MakeEntity(parse);
            m_List.add(p);
            m_Dic.put(p.Id, p);
            parse.Next();
        }

    }

    public List<P> GetList()
    {
        return m_List;
    }

    public P Get(int id)
    {
        if (m_Dic.containsKey(id))
        {
            return m_Dic.get(id);
        }
        return null;
    }
}
